/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.support.push;

import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.device.TagListResult;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import com.yanlight.support.PropertiesHolder;
import com.yanlight.support.event.OrderPayNotifyEvent;
import com.yanlight.support.pay.PayNotifyMessage;
import com.yanlight.type.PayType;
import com.yanlight.util.JsonUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

/**
 * @author haolw
 * @time 2017/11/06
 */
@Component
public class Pusher {
    private String APP_KEY = PropertiesHolder.get("jpush.appKey");
    private String APP_SECRET = PropertiesHolder.get("jpush.master.secret");

    private String APP_SUB_KEY = PropertiesHolder.get("jpush.sub.appKey");
    private String APP_SUB_SECRET = PropertiesHolder.get("jpush.sub.master.secret");
    private final String PAYMENT_TAG_PREFIX = PropertiesHolder.get("payment.tag.prefix");
    private final JPushClient client;
    private final JPushClient subClient;

    public Pusher() {
        ClientConfig clientConfig = ClientConfig.getInstance();
        client = new JPushClient(APP_SECRET, APP_KEY, null, clientConfig);
        subClient = new JPushClient(APP_SUB_SECRET, APP_SUB_KEY, null, clientConfig);
    }

    public boolean pushCompanyPayNotify(OrderPayNotifyEvent event) {
        String tag = PAYMENT_TAG_PREFIX + event.getCompanyId();
        String message = buildPayNotifyMessage(event);
        return doPushNotify(client, tag, message);
    }

    public boolean pushCompanyItemPayNotify(OrderPayNotifyEvent event) {
        String tag = PAYMENT_TAG_PREFIX + event.getCompanyId() + "." + event.getUserId();
        String message = buildPayNotifyMessage(event);
        return doPushNotify(subClient, tag, message);
    }

    private boolean doPushNotify(JPushClient client, String tag, String message) {

        try {
//            if (isNotEmptyTag(client, tag)) {
            PushPayload.Builder PayloadBuilder = PushPayload.newBuilder();
            PayloadBuilder.setPlatform(Platform.all());

            Audience audience = Audience.alias(tag);
            PayloadBuilder.setAudience(audience);

            PayloadBuilder.setMessage(Message.newBuilder().setContentType("pay").setMsgContent(message).build());
            PushPayload payload = PayloadBuilder.build();
            PushResult result = client.sendPush(payload);
            return result.isResultOK();
//            } else {
//                return false;
//            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private String buildPayNotifyText(OrderPayNotifyEvent event) {
        Integer payType = event.getPayType();
        Double fee = event.getFee();
        String result = "";
        if (PayType.ALIPAY.getType().equals(payType)) {
            result = "支付宝收款" + fee + "元";
        } else if (PayType.WXPAY.getType().equals(payType)) {
            result = "微信收款" + fee + "元";
        }
        return result;
    }

    private String buildPayNotifyMessage(OrderPayNotifyEvent event) {
        PayNotifyMessage message = new PayNotifyMessage();
        message.setOrderId(event.getOrderId());
        message.setPayType(event.getPayType());
        message.setSceneType(event.getSceneType());
        message.setPayTime(event.getPayTime());
        message.setFee(event.getFee());
        message.setText(buildPayNotifyText(event));
        return JsonUtil.toJson(message);
    }

    private boolean isNotEmptyTag(JPushClient client, String tag) {
        return !isEmptyTag(client, tag);
    }

    private boolean isEmptyTag(JPushClient client, String tag) {
        try {
            TagListResult tagList = client.getTagList();
            if (CollectionUtils.isEmpty(tagList.tags)) {
                return true;
            } else {
                return !tagList.tags.contains(tag);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
